//
//  OrigPubSelectBtnCell.m
//  ShrimpCrowdordering
//
//  Created by jimmy on 2018/8/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OrigPubSelectBtnCell.h"
#import "HorizontalPageFlowlayout.h"
#import "OrigPubSelectBtnCollectionCell.h"

@interface OrigPubSelectBtnCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    CGFloat _cellHeight;
    NSIndexPath * _indexPath;
    NSInteger _lines;
}

@property (nonatomic, assign) NSInteger  selecRow;

@property (nonatomic, strong) NSArray * dataAry;
@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation OrigPubSelectBtnCell

- (NSArray *)dataAry {
    if (_dataAry == nil) {
        _dataAry = [NSArray array];
    }
    return _dataAry;
}

+ (id)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height dataArray:(NSArray *)dataArray lines:(NSInteger)lines
{
    static NSString *cellID = @"OrigPubSelectBtnCell";
    
    OrigPubSelectBtnCell *cell = [[OrigPubSelectBtnCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID andIndexPath:indexPath andCellHeight:height dataArray:dataArray lines:lines];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height dataArray:(NSArray *)dataArray lines:(NSInteger)lines
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _cellHeight = height;
        _indexPath = indexPath;
        _lines = lines;
        self.dataAry = dataArray;
        [self setUIView];
    }
    
    return self;
}

- (void)selectIndex:(NSInteger)index
{
    self.selecRow = index;
    
    [self.collectionView reloadData];
}

- (void)setUIView
{
    HorizontalPageFlowlayout *layout = [[HorizontalPageFlowlayout alloc] initWithRowCount:_lines itemCountPerRow:4];
    [layout setColumnSpacing:10 rowSpacing:ScreenX375(10) edgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];

    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _cellHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = YES;
    //_collectionView.pagingEnabled = YES;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self addSubview:self.collectionView];

    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:[OrigPubSelectBtnCollectionCell class] forCellWithReuseIdentifier:@"colleCell"];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OrigPubSelectBtnCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colleCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor whiteColor];
    [cell setUIWithTitle:self.dataAry[indexPath.row]];
    if (indexPath.row == self.selecRow) {
        [cell isSelect:YES];
    }else{
       [cell isSelect:NO];
    }    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.selecRow = indexPath.row;
    [self.collectionView reloadData];
    if (self.selectIndexBlock) {
        self.selectIndexBlock(indexPath.row);
    }
}


@end
